// app.js
import agriknow from './utils/api.js'
const req=require('./utils/req')
App({
  onLaunch() {
    // 展示本地存储能力
    const logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)

    // 登录
    
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        console.log(res)
        var data={}
        data.code=res.code
        req.request('/ht/user/wxLogin',data,'POST').then(re=>{
          console.log(re)
          wx.setStorageSync('userLogin',re.data)
          wx.setStorageSync('userInfo',re.data)
        })
      }
    })

    this.getNavHeight()
  },
  // 用户页面访问埋点
  onShow(){
    // if(wx.getStorageSync('userLogin').userId&&!wx.getStorageSync('userInfo')){
    //   let data = {
    //     id:wx.getStorageSync('userLogin').userId
    //   }
    //   new agriknow().userHome(data).then(res=>{
    //     wx.setStorageSync('userInfo',res.data)
    //   })
    // }
    
    
  },
  globalData: {
    userInfo: null,
    height: 0,
    navContentHeight: 0,
    navContentWidth:0,
    navHeight: 0, 
    isFullSucreen:false
  },
  getNavHeight:function(){
    let that=this
    // 获取系统信息
    let systemInfo = wx.getSystemInfoSync();
    console.log(systemInfo)
    //判断是否ios
    if (systemInfo){
      if (systemInfo.system.indexOf('iOS') > -1) {
        that.globalData.is_ios=true
        
      } else {
        that.globalData.is_ios=false
      }
      }
      if (systemInfo.screenHeight - systemInfo.windowHeight - systemInfo.statusBarHeight - 32 > 72) {
        this.globalData.isFullSucreen = true
    }
    if (!systemInfo.StatusBarHeight) {
			systemInfo.StatusBarHeight = 20
		}
    console.log('this.globalData.isFullSucreen',this.globalData.isFullSucreen)
    wx.setStorageSync('systemInfo', systemInfo)
    // 胶囊按钮位置信息
    let menuButtonInfo = wx.getMenuButtonBoundingClientRect();
    
    let height = systemInfo.statusBarHeight;
    // 导航栏高度 = 状态栏到胶囊的间距（胶囊距上距离-状态栏高度） * 2 + 胶囊高度 + 状态栏高度
    let navHeight = (menuButtonInfo.top - systemInfo.statusBarHeight) * 2 + menuButtonInfo.height + systemInfo.statusBarHeight;
    let navContentHeight=navHeight-height
    let navContentWidth=menuButtonInfo.width
    
    that.globalData.navHeight=navHeight
    that.globalData.height=height
    that.globalData.navContentHeight=navContentHeight
    that.globalData.navContentWidth=navContentWidth

   wx.setStorageSync('height', height);
    wx.setStorageSync('navContentHeight', navContentHeight);
    wx.setStorageSync('navHeight', navHeight);
/*
    that.globalData.menuRight = systemInfo.screenWidth - menuButtonInfo.right;
    that.globalData.menuBotton = menuButtonInfo.top - systemInfo.statusBarHeight;
    that.globalData.menuHeight = menuButtonInfo.height;
*/
    that.globalData.ratio = systemInfo.pixelRatio;
    that.globalData.windowHeight=systemInfo.windowHeight
    that.globalData.windowWidth=systemInfo.windowWidth
  },
  //公共获取列表
  getList:function(url,data,type,that){
    req.request(url,data,type).then(res=>{
      console.log(res.data)
      var datas=res.data
      var list=datas.list || []
      //console.log(datas.list,'datas.list')
      if(url=='/ht/user/storeList' || url=='/ht/user/userCollectList'){
        list.map(item=>{
          item.distance=Number(Number(item.distance)/1000).toFixed(1)+'km'
        })
      }
      var lists=that.data.lists.concat(list)
      that.setData({
        lists:lists,
        isShow:true
      })

      var arr= lists.filter(ite=>{
        return ite.surplusTime>0 && ite.status==0
      })

      if(arr.length>0 && (url=='/ht/user/userOrderList')){
        that.countDown()
      }

      if(that.data.lists.length==0){
        that.setData({
          empty:true
        })
      }else{
        that.setData({
          empty:false
        })
      }
      
      if(that.data.page>=1 && datas.isLastPage){
      
        that.setData({
          isLoadingAll:true
        })
      }
    })
},
  onShareAppMessage() {

  },
  onShareTimeline(){
  },
  agriknow:new agriknow()
})
